﻿// <copyright file="BaseProvinceImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class BaseProvinceImporter : VisualCollectionImporter<BaseProvince>
    {
        private readonly int Visibility = 0;
        private readonly int Region = 1;
        private readonly int Name = 2;
        private readonly int Terrain = 3;
        private readonly int PublicInformation = 4;
        private readonly int GMInformation = 5;

        private IEnumerable<Terrain> terrains;

        public BaseProvinceImporter(DataWorkspace workspace)
            : base(workspace)
        {
        }

        protected override void FetchRequisites()
        {
            this.terrains = this.workspace.ApplicationData.Terrains.GetQuery().Execute();
        }

        protected override bool SkipEntity(int index)
        {
            if (this.headers[Name].HasValue)
            {
                if (this.entities.Any(e => e.Name.Equals(data[index + headers[Name].Value].Trim(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    return true;
                }
            }

            return false;
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[6];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Reg", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Region] = i;
                }
                else if (stringHeaders[i].StartsWith("Name", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Name] = i;
                }
                else if (stringHeaders[i].StartsWith("Terr", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Terrain] = i;
                }
                else if (stringHeaders[i].StartsWith("Public", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[PublicInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }
            }

            return headers;
        }

        protected override void FillEntity(BaseProvince entity, int index, int?[] headers, string[] data)
        {
            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value], out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[Region].HasValue)
            {
                entity.Region = data[index + headers[Region].Value];
            }

            if (headers[Name].HasValue)
            {
                entity.Name = data[index + headers[Name].Value];
            }

            if (headers[Terrain].HasValue)
            {
                entity.Terrain = terrains
                    .Where(
                        e =>
                        e.Name.Equals(data[index + headers[Terrain].Value],
                                      StringComparison.InvariantCultureIgnoreCase))
                    .FirstOrDefault();
            }

            if (headers[PublicInformation].HasValue)
            {
                entity.PublicInformation = data[index + headers[PublicInformation].Value];
            }

            if (headers[GMInformation].HasValue)
            {
                entity.GMInformation = data[index + headers[GMInformation].Value];
            }
        }
    }
}
